#!/bin/bash

# Check if build/test_cuda executable exists
if [ ! -f "build/test_cuda" ]; then
    echo "CUDA test executable not found. Building project..."
    ./build.sh --cuda --test
    if [ $? -ne 0 ]; then
        echo "Error building project. Exiting."
        exit 1
    fi
fi

echo "Running CUDA test..."
build/test_cuda | tee cuda_test.log

echo "Comparing CPU vs GPU norms..."

# Check for compare_norm.py in different locations
if [ -f "compare-diff/compare_norm.py" ]; then
    COMPARE_CMD="python compare-diff/compare_norm.py"
elif [ -f "compare_norm.py" ]; then
    COMPARE_CMD="python compare_norm.py"
elif command -v compare_norm.py &> /dev/null; then
    COMPARE_CMD="compare_norm.py"
elif [ -f "compare-diff/sh/compare_norm_simple.sh" ]; then
    COMPARE_CMD="bash compare-diff/sh/compare_norm_simple.sh"
elif [ -f "compare_norm_simple.sh" ]; then
    COMPARE_CMD="bash compare_norm_simple.sh"
else
    echo "Compare scripts not found. Skipping comparison."
    exit 0
fi

cat cuda_test.log | $COMPARE_CMD

echo "Comparison complete. Results saved in cuda_test.log" 